<template>
  <el-dialog
    width="750px"
    top="5vh"
    :title="editId ? '编辑公司' : '新增公司'"
    :visible.sync="dialogFormVisible"
    :close-on-click-modal="false"
  >
    <div v-loading="orcLoading" class="fdc">
      <p>公司信息</p>
      <div class="bus_info_max">
        <el-form ref="formData" :model="formData" label-width="80px">
          <el-form-item label="统一社会信用代码" prop="code">
            <el-input
              v-model.trim="formData.code"
              placeholder="请填写统一社会信用代码"
            ></el-input>
          </el-form-item>
          <el-form-item label="登记机关" prop="registrationAuthority">
            <el-input
              v-model.trim="formData.registrationAuthority"
              placeholder="请填写登记机关"
            ></el-input>
          </el-form-item>
          <el-form-item label="注册资金" prop="registeredCapital">
            <el-input
              v-model.trim="formData.registeredCapital"
              placeholder="请填写注册资金"
            ></el-input>
          </el-form-item>
          <el-form-item label="成立日期" prop="establishmentDate">
            <el-input
              v-model.trim="formData.establishmentDate"
              placeholder="请填写成立日期"
            ></el-input>
          </el-form-item>
          <el-form-item label="法人" prop="legalPerson">
            <el-input
              v-model.trim="formData.legalPerson"
              placeholder="请填写法人"
            ></el-input>
          </el-form-item>
          <el-form-item label="名称" prop="name">
            <el-input
              v-model.trim="formData.name"
              placeholder="请填写名称"
            ></el-input>
          </el-form-item>
          <el-form-item label="经营范围" prop="businessScope">
            <el-input
              v-model.trim="formData.businessScope"
              placeholder="请填写经营范围"
            ></el-input>
          </el-form-item>
          <el-form-item label="营业期限" prop="businessTerm">
            <el-input
              v-model.trim="formData.businessTerm"
              placeholder="请填写营业期限"
            ></el-input>
          </el-form-item>
          <el-form-item label="住所" prop="abode">
            <el-input
              v-model.trim="formData.abode"
              placeholder="请填写住所"
            ></el-input>
          </el-form-item>
          <el-form-item label="税号" prop="taxCode">
            <el-input
              v-model.trim="formData.taxCode"
              placeholder="请填写税号"
            ></el-input>
          </el-form-item>
          <el-form-item label="类型" prop="type">
            <el-input
              v-model.trim="formData.type"
              placeholder="请填写类型"
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
    </div>

    <div slot="footer" class="dialog-footer mt-30 jac">
      <el-button @click="dialogFormVisible = false">取 消</el-button>
      <el-button type="primary" @click="onSubmit">提交</el-button>
    </div>
  </el-dialog>
</template>

<script>
import authorityApi from '@/utils/tools/authorityApi/index.js'
export default {
  name: 'EditOrganizationDialog',
  props: {
    show: {
      type: Boolean,
      default: false
    },
    editId: {
      type: String,
      default: ''
    },
    addPid: {
      type: String,
      default: ''
    },
    pid: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      formData: {},
      formDataRules: {
        code: [
          { required: true, message: '请填写统一社会信用代码', trigger: 'blur' }
        ],
        registrationAuthority: [
          { required: true, message: '请填写登记机关', trigger: 'blur' }
        ],
        establishmentDate: [
          { required: true, message: '请填写成立日期', trigger: 'blur' }
        ],
        legalPerson: [
          { required: true, message: '请填写法人', trigger: 'blur' }
        ],
        businessTerm: [
          { required: true, message: '请填写营业期限', trigger: 'blur' }
        ],
        taxCode: [{ required: true, message: '请填写税号', trigger: 'blur' }]
      },
      orcLoading: false
    }
  },
  computed: {
    dialogFormVisible: {
      get() {
        return this.show
      },
      set(v) {
        this.$emit('update:show', v)
      }
    }
  },
  watch: {
    show(v) {
      if (v && this.editId) {
        this.initFormData()
        authorityApi.OrganizationPort.getByid({ id: this.editId }, (res) => {
          Object.assign(this.formData, res)
        })
      }
    }
  },
  created() {},
  methods: {
    /**初始化表单对象 */
    initFormData() {
      this.formData = {
        code: '', // 统一社会信用代码
        registrationAuthority: '', // 登记机关
        registeredCapital: '', // 注册资金
        establishmentDate: '', // 成立日期
        legalPerson: '', // 法人
        name: '', // 名称
        businessScope: '', // 经营范围
        businessTerm: '', // 营业期限
        abode: '', // 住所
        taxCode: '', // 税号
        type: '', // 类型
        businessLicensePath: '' // 营业执照
      }
    },
    onSubmit() {
      this.$refs.formData.validate(async (valid) => {
        if (!valid) return
        if (this.editId) {
          authorityApi.OrganizationPort.updateOrganization(this.formData, () =>
            this.successChange('更新成功')
          )
        } else {
          authorityApi.OrganizationPort.saveOrganization(
            Object.assign({}, this.formData, { pid: this.pid }),
            () => this.successChange('新增成功')
          )
        }
      })
    },
    successChange(text) {
      this.dialogFormVisible = false
      this.$message.success(text)
      this.$emit('change')
    }
  }
}
</script>

<style lang="less" scoped>
.pre_img {
  width: 250px;
  height: 150px;
}
</style>
